package wsi.input;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.rygh.core.util.UIo;
import org.rygh.semantic.gui.swing.input.Input;
import org.rygh.semantic.jena.SDataAccess;
import org.rygh.semantic.model.rdf.Entity;
import org.rygh.semantic.model.rdf.Fact;

import wsi.function.DirManager;
import wsi.function.Parameter;
import wsi.script.AppConfig;

@Deprecated
public class NaiveSelector extends InputSelector {
	private final static String CANDIDATE_METADATA = "candidate_metadata.txt";

	public NaiveSelector(DirManager dir) {
		super(dir, true);
	}

	@Override
	protected void compute() {
		assert false;

		List<Entity> entities = null;
		Parameter param = function.getParameter();

		int size = AppConfig.shared().getInputSize();

		if (param.getType() != null)
			entities = SDataAccess.selectByType(param.getType(), size);
		else {
			Fact fact = param.getFilterFact();

			if (fact.getCountOfVariables() == 1)
				entities = SDataAccess.select(param.getFilterFact(), size);
			else {
				if (fact.getSubject().getValue().equals("?e1"))
					entities = prepareMetadata(fact);
			}
		}

		if (entities == null)
			return;

		for (Entity e : entities) {
			Input input = new Input(e, null, e.getBodyString());
			detector.addInput(input);
		}
	}

	@Deprecated
	private List<Entity> prepareMetadata(Fact fact) {
		assert false;

		File f_input = new File(dir.getFunctionDir(), CANDIDATE_METADATA);

		if (!f_input.exists())
			return null;

		String content = UIo.readFile(f_input);
		String[] lines = content.split("\r\n");

		List<Entity> entities = new ArrayList<>();
		int size = AppConfig.shared().getInputSize();

		for (int i = 0; i < Math.min(lines.length, size); i++) {
			String s = lines[i];
			Entity subj = new Entity("y:" + s);
			Fact f = new Fact(subj, fact.getPredicate(), fact.getObject());

			List<Entity> list = SDataAccess.select(f, size);

			entities.addAll(list);
			System.out.println("Obtained entities " + i + ", " + list);
		}

		return entities;
	}

	@Deprecated
	void prepareMetadata(File inputFile) {
		List<Entity> entities = new ArrayList<>();
		String id_pred = function.getIdPredicate();
		int size = AppConfig.shared().getInputSize();

		File f_input = new File(dir.getFunctionDir(), CANDIDATE_METADATA);

		if (f_input.exists()) {
			String content = UIo.readFile(f_input);
			String[] lines = content.split("\r\n");

			for (int i = 0; i < Math.min(lines.length, size); i++) {
				String s = lines[i];

				Fact f = new Fact("y:" + s, id_pred, "?x");

				List<Entity> list = SDataAccess.select(f, size);

				entities.addAll(list);
				System.out.println("Obtained entities " + i + ", " + list);
			}
		} else {
			Fact f = new Fact("?s", id_pred, "?x");

			List<Entity> list = SDataAccess.select(f, size);

			entities.addAll(list);
		}

		StringBuilder sbuilder = new StringBuilder();

		for (Entity i : entities) {
			sbuilder.append(i.getBodyString());
			sbuilder.append("\r\n");
		}

		UIo.saveFile(inputFile, sbuilder.toString());
	}

}
